<?php

/**
 * YML Parser
 *
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
* @version 0.3
 */
 
require('lib/spyc/spyc.php');

/**
 * CSS Tidy used to minification of CSS 
 *
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 1.3
 */

require('lib/csstidy/csstidy.php');

/**
 * Asset Packager Class
 *
 * A tool to merge and compress CSS based on the Ruby on Rails plugin
 * available here: http://synthesis.sbecker.net/pages/asset_packager
 *
 * PHP Asset Packager is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 *
 * PHP Asset Packager is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You can get a copy of the GNU General Public License by writing to 
 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 *
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * An online version should be available here: http://morethanseven.net/presents/asset_packager
 *
 * @package PHP Asset Packager
 * @author Gareth Rushgrove (gareth at morethanseven dot net) 2007
 * @version 0.1
 */

class Asset_packager {
  
  /**
   * Default stylesheet path
   * @var string
   * @access public
   */
  
  const STYLESHEET = 'stylesheets/';

  /**
   * Default configuration file
   * @var string
   * @access public
   */

  const CONFIG = 'config/asset_packager.yml';

  /**
   * Output links to stylesheets dependent on environment
   * @param array $config
   * @access public
   * @return string
   * @version 0.1
   */

  public static function stylesheet_link_merged($config = null) {
          
    if (!$config) {
      if (!isset($config['config_location'])) {
        $config['config_location'] = self::CONFIG;
      }
      if (!isset($config['stylesheet_location'])) {
        $config['stylesheet_location'] = self::STYLESHEET;
      }
    }
          
    $assets = Spyc::YAMLLoad($config['config_location']);
    $output = '';
    if (defined("ENVIRONMENT") && ENVIRONMENT == "production") {
      foreach ($assets['stylesheets'] as $target => $components) {
        $output .= '<link rel="stylesheet" href="' . $config['stylesheet_location'] . $target . '.css" type="text/css" media="screen" />' . "\n";
      }
    } else {
      foreach ($assets['stylesheets'] as $target => $components) {
        foreach ($components as $component) {
          $output .= '<link rel="stylesheet" href="' . $config['stylesheet_location'] . $component . '.css" type="text/css" media="screen" />' . "\n";
        }
      }
    }
    echo $output;
  }
  
  /**
   * Compile CSS files according to definition provided
   * @param array $config
   * @access public
   * @return string
   * @version 0.1
   */
  
  public static function build_all($config = null) {
    
    if (!$config) {
      if (!isset($config['config_location'])) {
        $config['config_location'] = self::CONFIG;
      }
      if (!isset($config['stylesheet_location'])) {
        $config['stylesheet_location'] = self::STYLESHEET;
      }
    }
    
    $output = "Asset packager running\n";
    $assets = Spyc::YAMLLoad($config['config_location']);
    foreach ($assets['stylesheets'] as $target => $components) {
      $complete = '';
      foreach ($components as $component) {
       $complete .= file_get_contents($config['stylesheet_location'] . $component . '.css');
      }
      $css = new csstidy();
      $css->parse($complete);
      $complete = $css->print->plain();
      $filename = $config['stylesheet_location'] . $target . '.css'; 
      if ($handle = fopen($filename, 'w')) {
        if (!fwrite($handle, $complete) === FALSE) {
          $output .= $target . ".css file created\n";
        }
        fclose($handle);
      }
    }
    echo $output;
  }
 
}